using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Sharpie.Libs;
using SharpieAndroid.Libs;

namespace SharpieAndroid.States
{
	public class MainMenu : DrawableGameComponent
	{
		ButtonMenu mainMenu;
		SpriteBatch spriteBatch;
		Texture2D logo;
		float LogoScale = 0.6F;
		double LogoRadians = 1D;
		string GameVersion = System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).FileVersion;
		SpriteFont FontSmall, FontNormal, FontBigB, FontNormalB;
		DialogBox instructDB;
		string[] instructText = new string[] {
			"Sharpie",
			Locale.String("VERSION") + " ",
			Locale.String("STEERING") + ":",
			Locale.String("ARROW_KEYS") + " - " + Locale.String("SNAKE_DIRECTION"),
			"Back" + " - " + Locale.String("PAUSE_MENU"),
			Locale.String("COLLECT_FOOD_TIP"),
			Locale.String("COLLECT_POINTS_TIP"),
			Locale.String("SURVIVE_TIP"),
			Locale.String("ENJOY_TIP")
		};

		public MainMenu(Game game)
			: base(game) { }

		public override void Initialize()
		{
			base.Initialize();
			mainMenu = new ButtonMenu(new Point(0, GraphicsDevice.Viewport.Height), Game);
			mainMenu.AddButton(LabelButton.Type.NORMAL, "{SCORES}");
			mainMenu.AddButton(LabelButton.Type.NORMAL, "{INSTRUCTION}");
			mainMenu.AddButton(LabelButton.Type.NORMAL, "{SETTINGS}");
			mainMenu.AddButton(LabelButton.Type.NORMAL, "{NEW_GAME}", Color.LimeGreen, Color.White);
			instructDB = new DialogBox(Game);
			instructDB.Width = 300;
			instructDB.Height = 220;
		}

		protected override void LoadContent()
		{
			base.LoadContent();
			spriteBatch = new SpriteBatch(GraphicsDevice);
			logo = Game.Content.Load<Texture2D>("Images\\gamelogo");
			FontSmall = Game.Content.Load<SpriteFont>("Fonts\\11pt");
			FontNormal = Game.Content.Load<SpriteFont>("Fonts\\16pt");
			FontNormalB = Game.Content.Load<SpriteFont>("Fonts\\16ptB");
			FontBigB = Game.Content.Load<SpriteFont>("Fonts\\20ptB");
		}

		public override void Update(GameTime gameTime)
		{
			base.Update(gameTime);
			LogoRadians += 0.06D;
			LogoScale += 0.0005F * (float)Math.Sin(LogoRadians);
			mainMenu.Update(gameTime);

			if (instructDB.Visible)
			{
				instructDB.Update(gameTime);
			}
			else
			{
				if (!mainMenu.Enabled) mainMenu.Enabled = true;
			}
			switch (mainMenu.ClickedPos)
			{
				case 1:
					instructDB.Show();
					mainMenu.Enabled = false;
					break;
				case 2:
					StateManager.ChangeState("settmenu", Screen.HideStyle.SlideRight);
					break;
				case 3:
					StateManager.ChangeState("mapmenu", Screen.HideStyle.Fade);
					break;
			}
		}

		public override void Draw(GameTime gameTime)
		{
			base.Draw(gameTime);
			spriteBatch.Begin();
			spriteBatch.Draw(logo, new Vector2(GraphicsDevice.Viewport.Width / 2, logo.Height / 3), null, Color.White, 0F, new Vector2(logo.Width / 2, logo.Height / 2), LogoScale, SpriteEffects.None, 0F);
			if (GameVersion != "")
				Text.DrawShadowedText(spriteBatch, FontSmall, Locale.String("VERSION") + " " + GameVersion, new Vector2((int)2, (int)0), Color.White);
			spriteBatch.End();
			mainMenu.Draw(gameTime);
			if (instructDB.Visible)
			{
				instructDB.Draw(gameTime);
				spriteBatch.Begin();
				spriteBatch.DrawString(FontBigB, instructText[0], new Vector2(instructDB.GetRectangle().Center.X, instructDB.GetRectangle().Center.Y), Color.White, 0F, new Vector2(instructDB.GetRectangle().Width / 2 - 15, instructDB.GetRectangle().Height / 2 - 15), instructDB.Scale, SpriteEffects.None, 0F);
				spriteBatch.DrawString(FontSmall, instructText[1] + GameVersion, new Vector2(instructDB.GetRectangle().Center.X, instructDB.GetRectangle().Center.Y), Color.White, 0F, new Vector2(instructDB.GetRectangle().Width / 2 - 15, instructDB.GetRectangle().Height / 2 - 15 - FontBigB.LineSpacing), instructDB.Scale, SpriteEffects.None, 0F);
				spriteBatch.DrawString(FontNormalB, instructText[2], new Vector2(instructDB.GetRectangle().Center.X, instructDB.GetRectangle().Center.Y), Color.White, 0F, new Vector2(instructDB.GetRectangle().Width / 2 - 15, instructDB.GetRectangle().Height / 2 - 15 - FontBigB.LineSpacing - (int)(FontNormal.LineSpacing * 1.2F)), instructDB.Scale, SpriteEffects.None, 0F);
				spriteBatch.DrawString(FontNormal, instructText[3], new Vector2(instructDB.GetRectangle().Center.X, instructDB.GetRectangle().Center.Y), Color.White, 0F, new Vector2(instructDB.GetRectangle().Width / 2 - 15, instructDB.GetRectangle().Height / 2 - 15 - FontBigB.LineSpacing - (int)(FontNormal.LineSpacing * 1.1F) - FontNormalB.LineSpacing), instructDB.Scale, SpriteEffects.None, 0F);
				spriteBatch.DrawString(FontNormal, instructText[4], new Vector2(instructDB.GetRectangle().Center.X, instructDB.GetRectangle().Center.Y), Color.White, 0F, new Vector2(instructDB.GetRectangle().Width / 2 - 15, instructDB.GetRectangle().Height / 2 - 15 - FontBigB.LineSpacing - (int)(FontNormal.LineSpacing * 2.0F) - FontNormalB.LineSpacing), instructDB.Scale, SpriteEffects.None, 0F);
				spriteBatch.End();
			}
		}
	}
}